//
//  ViewController.m
//  PostVerify
//
//  Created by Develop on 2018/11/6.
//  Copyright © 2018 Develop. All rights reserved.
//

#import "ViewController.h"
#import "GMHttpInvoker.h"

@interface ViewController()<NSTableViewDelegate,NSTableViewDataSource>
@property (weak) IBOutlet NSTextField *TextUrl;
@property (weak) IBOutlet NSScrollView *ScrollView;
@property (weak) IBOutlet NSButton *addData;
@property (weak) IBOutlet NSButton *Submit;
@property (weak) IBOutlet NSTextField *Result;
@property (weak) IBOutlet NSButton *BtnPost;
@property (weak) IBOutlet NSButton *BtnGet;
@property (weak) IBOutlet NSTextField *keyText;
@property (weak) IBOutlet NSTextField *valueText;

@property (strong , nonatomic) NSMutableDictionary* dic;

@property Boolean isPost;

@end

@implementation ViewController
{
    NSTableView * _tableView;
    NSMutableArray * _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    _dic = [NSMutableDictionary dictionary];
    _dataArray = [NSMutableArray array];
//    for (int i=0; i<20; i++) {
//        [_dataArray addObject:[NSString stringWithFormat:@"%d行数据",i]];
//    }
    _ScrollView.hasVerticalScroller  = YES;
    
    _tableView = [[NSTableView alloc]initWithFrame:self.view.bounds];
    NSTableColumn * column = [[NSTableColumn alloc]initWithIdentifier:@"test"];
    [_tableView addTableColumn:column];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView reloadData];
    _ScrollView.contentView.documentView = _tableView;
}

-(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView{
    return _dataArray.count;
}

-(id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
    return _dataArray[row];
}
- (IBAction)radioBtnClick:(NSButton *)sender {
    switch (sender.tag) {
        case 0:
            _isPost = YES;
            break;
        case 1:
            _isPost = NO;
            break;
        default:
            break;
    }
}
- (IBAction)addOnClick:(id)sender {
    if(_keyText.stringValue!=Nil&&[_keyText.stringValue length]>0){
        [_dic setObject:_valueText.stringValue forKey:_keyText.stringValue];
        [_dataArray addObject:[NSString stringWithFormat:@"%@ : %@",_keyText.stringValue,_valueText.stringValue]];
        [_tableView reloadData];
        _keyText.stringValue=@"";
        _valueText.stringValue=@"";
    }
}
- (IBAction)submitOnclick:(id)sender {
    if (_isPost) {
        [self postTest];
    } else {
        [self getTest];
    }
}

-(void)postTest{
    [[GMHttpInvoker shareManager] postAsync:_TextUrl.stringValue withDictionary:_dic withSuccessBlock:^(NSURLResponse *response, id responseObject) {
        NSLog(@"%@",responseObject);
        self->_Result.stringValue = responseObject;
    } withFailureBlock:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

-(void)getTest{
    [[GMHttpInvoker shareManager] getWithUrl:_TextUrl.stringValue withDictionary:_dic withSuccessBlock:^(NSURLResponse *response, id responseObject) {
        NSLog(@"%@",responseObject);
        self->_Result.stringValue = responseObject;
    } withFailureBlock:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

@end
